﻿#include <grpcpp/grpcpp.h>
#include "user.grpc.pb.h"
#include <iostream>

using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using user::UserRequest;
using user::UserResponse;
using user::UserService;

class UserServiceImpl final : public UserService::Service {
public:
    Status GetUser(ServerContext* context, const UserRequest* request,
                   UserResponse* response) override {
        int id = request->id();
        response->set_id(id);
        response->set_name("Alice");
        response->set_age(25);
        std::cout << "Received request for user id: " << id << std::endl;
        return Status::OK;
    }
};

void RunServer() {
    std::string server_address("0.0.0.0:50051");
    UserServiceImpl service;

    ServerBuilder builder;
    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
    builder.RegisterService(&service);
    std::unique_ptr<Server> server(builder.BuildAndStart());
    std::cout << "Server listening on " << server_address << std::endl;
    server->Wait();
}

int main() {
    RunServer();
    return 0;
}
